package com.base.cn.platform.os.controller.user.sign;

import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.manage.user.signin.CusUserSignInService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户签到接口
 *
 * @author s.li
 * @create 2018-08-28-14:39
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserSignInController extends ApiBaseController {

    @Autowired
    private CusUserSignInService cusUserSignInService;
    @Autowired
    private CusUserService userService;

    /**
     * 保存用户签到
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/saveSignIn")
    public Map<String,Object> saveSignIn(@RequestParam("userId")BigDecimal userId){
        Map<String,Object> userSignIn = new HashMap<>();
        userSignIn.put("userId",userId);
        userSignIn.put("signDate",new Date());
        return cusUserSignInService.saveUserSignIn(userSignIn);
    }

    /**
     * 用户当月签到列表
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/querySignInList")
    public Map<String,Object> querySignInList(@RequestParam("userId") BigDecimal userId,
                                              @RequestParam("year") int year,
                                              @RequestParam("month") int month){
        List<Map<String, Object>> userMonthSignList = cusUserSignInService.findUserMonthSignList(userId,String.valueOf(year),String.valueOf(month));
        int mothNum = 0;
        if(ObjectUtils.isNotEmpty(userMonthSignList)){
            mothNum = userMonthSignList.size();
        }
        Map<String,Object> resultMap = ResultUtil.SUCCESS(userMonthSignList);
        resultMap.put("mothNum",mothNum);
        return resultMap;
    }

    /**
     * 用户签到总天数
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/totalSignInNum")
    public Map<String,Object> totalSignInNum(@RequestParam("userId") BigDecimal userId){
        Map<String, Object> userInfo = userService.userInfo(userId, false, false, false, false, false, false, false);
        Object signNum = userInfo.get("signNum");
        return ResultUtil.SUCCESS(signNum);
    }

    /**
     * ⽤户今天是否签到
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/processingCheckIn")
    public Map<String,Object> processingCheckIn(@RequestParam("userId") BigDecimal userId){
        boolean check = cusUserSignInService.checkUserIsSign(userId,DateUtils.format(new Date(),"yyyy-MM-dd"));
        if(check){
            return ResultUtil.SUCCESS("已经签到过");
        }
        return ResultUtil.ERROR("未签到过");
    }

}
